package com.hitnet.hitnet.pic;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.hitnet.hitnet.R;
import com.hitnet.hitnet.pic.MyDialog.ClickListener;

import java.util.ArrayList;
import java.util.List;

public class ShowPrimaryImages extends Activity
{
	private BaseViewPager imageViewPager;
	private Button delete; //
//	private Button confirm; //
	//private String imgPath; //
	private int currentPosition;//
	private List<String> selectedImgsPath;//
	private MyPageAdapter adapter;
	private TextView current_pic;
	private TextView total_pic;

	/**
	 *
	 * 
	 * @return
	 */
	public List<String> getConfirmImgs()
	{
		return selectedImgsPath;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_photo);

		selectedImgsPath = getIntent().getStringArrayListExtra("selectedImgsPath");
		//imgPath = getIntent().getStringExtra("imgPath");

		currentPosition = getIntent().getIntExtra("currentPosition", 0);
		findViews();
		current_pic = (TextView)this.findViewById(R.id.current_pic);
		total_pic = (TextView)this.findViewById(R.id.total_pic);
		
		total_pic.setText("/"+String.valueOf(selectedImgsPath.size()));
		current_pic.setText(String.valueOf(currentPosition+1));
		
		
		adapter = new MyPageAdapter(ShowPrimaryImages.this, selectedImgsPath);
		imageViewPager.setAdapter(adapter);
		imageViewPager.setCurrentItem(currentPosition);
		imageViewPager.setOnPageChangeListener(new ViewPagerChangeListener());
		imageViewPager.setEnabled(false);
		setListeners();
	}
	
	private void setListeners(){
		delete.setOnClickListener(new View.OnClickListener()
		{
			/**
			 * 1��2��3��4��5 ���ɾ��1������ʾ2 ���ɾ��5�����һ�ţ�������ʾ4
			 */
			public void onClick(View v)
			{
				final MyDialog d = new MyDialog(ShowPrimaryImages.this, "Primary");
				d.show();
				d.setOnClickListener(new ClickListener() {
					public void onButtonOk() {
						if(selectedImgsPath.size() == 1){
							NativeImageLoader.mSelectList.clear();//
							selectedImgsPath.clear();
						}else{
							String str = selectedImgsPath.get(currentPosition);
							NativeImageLoader.mSelectList.remove(str);//
							selectedImgsPath.remove(currentPosition);
							if(currentPosition==1){
								current_pic.setText(String.valueOf(currentPosition));
							}
							
							total_pic.setText("/"+String.valueOf(selectedImgsPath.size()));
						}
						
						adapter = new MyPageAdapter(ShowPrimaryImages.this, selectedImgsPath);
						imageViewPager.setAdapter(adapter);

						Intent data = new Intent();
						data.putStringArrayListExtra("selectedpath", (ArrayList)selectedImgsPath);
						ShowPrimaryImages.this.setResult(RESULT_OK, data);

						if (selectedImgsPath.size() != 0)
						{
							imageViewPager.setCurrentItem(currentPosition);
						} else
						{
							/**
							 * finish
							 */
							finish();
						}
						d.dismiss();
					}
					public void onButtonCancel() {
						d.dismiss();
					}
				});
				
			}
		});
	}

	class ViewPagerChangeListener implements ViewPager.OnPageChangeListener
	{

		public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
		{
			currentPosition = position;
		}

		public void onPageSelected(int position)
		{
			currentPosition = position;
			current_pic.setText(String.valueOf(position+1));
		}

		public void onPageScrollStateChanged(int state)
		{

		}
	}

	private void findViews()
	{
		imageViewPager = (BaseViewPager) findViewById(R.id.viewpager);
		delete = (Button) findViewById(R.id.photo_bt_exit);
//		confirm = (Button) findViewById(R.id.photo_bt_enter);
	}


}
